//index.js
import { apiUrls } from '../../utils/api'

Page({
  data: {
    cdnUrl: 'http://static.xunyi.mobi/',
    inputShowed: false,
    inputVal: "",
    items: [],
    cover: '/images/1.png',
    windowHeight: 0,
    scrollTop: 0,
    animationLoad: {},
    animationData: {},
    hidden: true,
  },
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      },
    });
  },
  onShow: function () {
    let that = this;
    GetList(that);

  },
  onPullDownRefresh: function () {
    let that = this;
    GetList(that);
    wx.stopPullDownRefresh();
  },
  showInput(e) {
    this.setData({
      inputShowed: true
    });
  },
  hideInput(e) {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput(e) {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping(e) {
    let that = this;
    that.setData({
      inputVal: e.detail.value
    });
    getLocation((lat, lng) => {
      wx.request({
        url: apiUrls.getShopList,
        method: 'GET',
        data: {
          lng: lng,
          lat: lat,
          search: e.detail.value
        },
        success: function (res) {
          that.setData({
            items: res.data.data
          });
        },
        fail: function (err) {

        }
      });
    });
  },
  skipShop(e) {
    let that = this;
    let shopId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/index?id=${shopId}`,
    });
  },
  lower(e) {
    let that = this;
    // GetList(that);  
  },
  upper(e) {
    let that = this;
    GetList(that);
  },
  scroll: function (e) {
    let that = this;
    let scrollTop = e.detail.scrollTop
    that.setData({
      scrollTop: scrollTop
    });
  }
})
function getLocation(cb) {
  wx.getLocation({
    type: 'gcj02',
    success: function (res) {
      let lat = res.latitude;
      let lng = res.longitude;
      cb && cb(lat, lng);
    }
  })
}
const GetList = function (that) {
  that.setData({
    hidden: false
  });
  let animation = wx.createAnimation({
    duration: 1000,
    timingFunction: 'ease',
  });
  that.animation = animation;
  setInterval(function(){
    animation.rotate(360).step();
    that.setData({
      animationLoad: animation.export()
    });
  },1000);
  
  getLocation((lat, lng) => {
    wx.request({
      url: apiUrls.getShopList,
      data: {
        lng: lng,
        lat: lat
      },
      success: function (res) {
        let items = res.data.data.map((item) => {
          // let distance = 0;
          // if (item.distance < 10) {
          //   distance = 0.01
          // } else {
          //   distance = (item.distance / 1000).toFixed(2);
          // }
          return {
            id: item.id,
            name: item.name,
            cover: item.cover,
            distance: item.distance_km,
            is_active: item.is_active
          }
        });

        that.setData({
          items: items,
          hidden: true
        });
      }
    });
  });
}
function animation(e, cb) {
  let animation = wx.createAnimation({
    duration: 1000,
    timingFunction: 'ease',
  })
  cb && cb(animation);
}
